<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>逻辑运算符 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="chapter07.html" />
    
    
    <link rel="prev" href="chapter05.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="6"
        data-chapter-title="逻辑运算符"
        data-filepath="chapter06.md"
        data-basepath="."
        data-revision="Mon Feb 03 2020 12:30:22 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        整数类型操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据类型转换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        算数运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        赋值运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        比较运算符
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        逻辑运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        位运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        运算符优先级
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;">&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7B26;</h2>
<table style="text-align:center">
<tr><th>&#x8FD0;&#x7B97;&#x7B26;</th><th>&#x903B;&#x8F91;&#x8868;&#x8FBE;&#x5F0F;</th><th>&#x63CF;&#x8FF0;</th><th>&#x5B9E;</th></tr>
<tr><td>and</td><td>x and y</td><td>&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x8FD0;&#x7B97;&#x6570;&#x662F;False&#xFF0C;&#x7ED3;&#x679C;&#x5C31;&#x662F;False;<br>&#x53EA;&#x6709;&#x6240;&#x6709;&#x7684;&#x8FD0;&#x7B97;&#x6570;&#x90FD;&#x4E3A;True&#x65F6;&#xFF0C;&#x7ED3;&#x679C;&#x624D;&#x662F;True<br>&#x505A;&#x53D6;&#x503C;&#x8FD0;&#x7B97;&#x65F6;&#xFF0C;&#x53D6;&#x7B2C;&#x4E00;&#x4E2A;&#x4E3A;False&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x6240;&#x6709;&#x7684;&#x503C;&#x90FD;&#x4E3A;True,&#x53D6;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x503C;&#x3002;</td><td>True and True and False--&gt;&#x7ED3;&#x679C;&#x4E3A;False<br>True and True and True--&gt;&#x7ED3;&#x679C;&#x4E3A;True<br> 1 and 0 and 2--&gt;&#x7ED3;&#x679C;&#x662F;0;1 and 2 and 3--&gt;&#x7ED3;&#x679C;&#x662F;3</td></tr>
<tr><td>or</td><td>x or y</td><td>&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x8FD0;&#x7B97;&#x6570;&#x662F;True&#xFF0C;&#x7ED3;&#x679C;&#x5C31;&#x662F;True;<br>&#x53EA;&#x6709;&#x6240;&#x6709;&#x7684;&#x8FD0;&#x7B97;&#x6570;&#x90FD;&#x4E3A;False&#x65F6;&#xFF0C;&#x7ED3;&#x679C;&#x624D;&#x662F;False<br>&#x505A;&#x53D6;&#x503C;&#x8FD0;&#x7B97;&#x65F6;&#xFF0C;&#x53D6;&#x7B2C;&#x4E00;&#x4E2A;&#x4E3A;True&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x6240;&#x6709;&#x7684;&#x503C;&#x90FD;&#x4E3A;False,&#x53D6;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x503C;&#x3002;</td><td>False or False or True--&gt;&#x7ED3;&#x679C;&#x4E3A;True<br>False or False or False--&gt;&#x7ED3;&#x679C;&#x4E3A;False<br> 1 or 0 or 2--&gt;&#x7ED3;&#x679C;&#x662F;1;0 or None or () or &apos;&apos; or {} --&gt;&#x7ED3;&#x679C;&#x662F;{}</td></tr>
<tr><td>not</td><td>not x</td><td>&#x5E03;&#x5C14;&quot;&#x975E;&quot; - &#x5982;&#x679C; x &#x4E3A; True&#xFF0C;&#x8FD4;&#x56DE; False &#x3002;&#x5982;&#x679C; x &#x4E3A; False&#xFF0C;&#x5B83;&#x8FD4;&#x56DE; True&#x3002;</td><td> not(a and b) &#x8FD4;&#x56DE; False </td></tr>
</table>


<h2 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;:</h2>
<pre><code class="lang-python">a = <span class="hljs-number">34</span>
b = <span class="hljs-number">12</span>

print((a &gt; b) <span class="hljs-keyword">and</span> (b &gt; <span class="hljs-number">10</span>) <span class="hljs-keyword">and</span> (a &gt; <span class="hljs-number">30</span>) <span class="hljs-keyword">and</span> (a &gt; <span class="hljs-number">50</span>)) 
print((a &gt; b) <span class="hljs-keyword">and</span> (b &gt; <span class="hljs-number">10</span>) <span class="hljs-keyword">and</span> (a &gt; <span class="hljs-number">30</span>) <span class="hljs-keyword">and</span> (a &lt; <span class="hljs-number">50</span>)) 
print(a <span class="hljs-keyword">and</span> b <span class="hljs-keyword">and</span> <span class="hljs-string">&apos;hello&apos;</span> <span class="hljs-keyword">and</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> <span class="hljs-number">123</span>)
print(a <span class="hljs-keyword">and</span> b <span class="hljs-keyword">and</span> <span class="hljs-string">&apos;hello&apos;</span> <span class="hljs-keyword">and</span> <span class="hljs-number">123</span> <span class="hljs-keyword">and</span> <span class="hljs-string">&apos;hi&apos;</span>)


print((a &gt; <span class="hljs-number">50</span>) <span class="hljs-keyword">or</span> (b &lt; <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> a &gt; b <span class="hljs-keyword">or</span> (a == b))
print((a &gt; <span class="hljs-number">50</span>) <span class="hljs-keyword">or</span> (b &lt; <span class="hljs-number">10</span>) <span class="hljs-keyword">or</span> a &lt; b <span class="hljs-keyword">or</span> (a == b))
print(<span class="hljs-number">0</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">None</span> <span class="hljs-keyword">or</span> a <span class="hljs-keyword">or</span> <span class="hljs-string">&apos;&apos;</span>)
print(<span class="hljs-number">0</span> <span class="hljs-keyword">or</span> <span class="hljs-string">&quot;&quot;</span> <span class="hljs-keyword">or</span> <span class="hljs-string">&apos;&apos;</span> <span class="hljs-keyword">or</span> <span class="hljs-keyword">None</span>)
</code></pre>
<h2 id="&#x63D0;&#x9AD8;">&#x63D0;&#x9AD8;:</h2>
<p>&#x9762;&#x8BD5;&#x9898;&#xFF1A;&#x4E00;&#x4E0B;&#x4EE3;&#x7801;&#x7684;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x4F1A;&#x6709;&#x8FD9;&#x6837;&#x7684;&#x8F93;&#x51FA;&#x3002;</p>
<pre><code class="lang-python">a = <span class="hljs-number">34</span>

a &gt; <span class="hljs-number">10</span> <span class="hljs-keyword">and</span> print(<span class="hljs-string">&apos;hello world&apos;</span>)
a &lt; <span class="hljs-number">10</span> <span class="hljs-keyword">and</span> print(<span class="hljs-string">&apos;hello world&apos;</span>)

a &gt;<span class="hljs-number">10</span> <span class="hljs-keyword">or</span> print(<span class="hljs-string">&apos;&#x4F60;&#x597D;&#x4E16;&#x754C;&apos;</span>)
a &lt;<span class="hljs-number">10</span> <span class="hljs-keyword">or</span> print(<span class="hljs-string">&apos;&#x4F60;&#x597D;&#x4E16;&#x754C;&apos;</span>)
</code></pre>
<p>&#x601D;&#x8003;&#xFF1A;</p>
<ol>
<li>&#x903B;&#x8F91;&#x8FD0;&#x7B97;&#x7684;&#x77ED;&#x8DEF;&#x95EE;&#x9898;</li>
<li>&#x903B;&#x8F91;&#x4E0E;&#x8FD0;&#x7B97;&#x548C;&#x903B;&#x8F91;&#x6216;&#x8FD0;&#x7B97;&#x53D6;&#x503C;&#x65F6;&#x4E3A;&#x4EC0;&#x4E48;&#x662F;&#x90A3;&#x79CD;&#x89C4;&#x5219;&#x3002;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="chapter05.html" class="navigation navigation-prev " aria-label="Previous page: 比较运算符"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="chapter07.html" class="navigation navigation-next " aria-label="Next page: 位运算符"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
